package trlygjj.tanruiliyigenjinjin.web.controller;

import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import trlygjj.tanruiliyigenjinjin.query.ChartQueryObject;
import trlygjj.tanruiliyigenjinjin.service.IChartService;
import trlygjj.tanruiliyigenjinjin.service.ITuitionService;
import trlygjj.tanruiliyigenjinjin.util.PermissionName;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by user on 2018/7/11.
 */
@Controller
@RequestMapping("chart")
public class ChartController {

    @Autowired
    private IChartService chartService;

    @Autowired
    private ITuitionService tuitionService;

    /**
     * 提供一个默认不输入后缀,直接可以跳转到具有分页的列表页面
     * @return
     */
    @RequiresPermissions("chart:index")
    @PermissionName("报表管理主页")
    @RequestMapping("")
    public String index(){
        System.out.println("-----------------1-----------");
        return "chart/list";
    }

    @RequiresPermissions("chart:queryTuition")
    @PermissionName("报表管理数据")
    @RequestMapping("queryTuition")
    @ResponseBody
    public List<Map<String, Object>> queryTuition(ChartQueryObject qo){
        List<Map<String, Object>> maps = chartService.selectProfessionalStuentChart(qo);
        return maps;
    }

    @RequiresPermissions("chart:queryGroupType")
    @PermissionName("报表管理分组类型")
    @RequestMapping("queryGroupType")
    @ResponseBody
    public List<Map<String, String>> queryGroupType(){
        List<Map<String, String>> list = ChartQueryObject.list;
        return list;
    }

    @RequiresPermissions("chart:bar")
    @PermissionName("报表管理柱形图")
    @RequestMapping("bar")
    public String bar(Model model,ChartQueryObject qo){
        model.addAttribute("groupByType", ChartQueryObject.map.get(qo.getGroupByType()));
        List<Map<String, Object>> selectBar = chartService.selectProfessionalStuentChart(qo);
        List<Object> groupByTypes = new ArrayList<>();
        List<Object> totalPaid = new ArrayList<>();
        List<Object> totalUnPaid = new ArrayList<>();
        for(Map<String, Object> map : selectBar){
            groupByTypes.add(map.get("groupByType"));
            totalPaid.add(map.get("totalPaid"));
            totalUnPaid.add(map.get("totalUnPaid"));
        }
        model.addAttribute("groupByTypes", JSON.toJSONString(groupByTypes));
        model.addAttribute("totalPaid", JSON.toJSONString(totalPaid));
        model.addAttribute("totalUnPaid", JSON.toJSONString(totalUnPaid));
        return "chart/barList";
    }

    @RequiresPermissions("chart:pie")
    @PermissionName("报表管理饼形图")
    @RequestMapping("pie")
    public String pie(Model model,ChartQueryObject qo){
        model.addAttribute("groupByType", ChartQueryObject.map.get(qo.getGroupByType()));
        List<Map<String,Object>> selectPar = chartService.selectProfessionalStuentChart(qo);
        List<Object> groupByTypes = new ArrayList<>();
        List<Object> datas = new ArrayList<>();
        for(Map<String,Object> map : selectPar){
            groupByTypes.add(map.get("groupByType"));
            Map<String,Object> data = new HashMap<>();
            data.put("value",map.get("totalPaid"));
            data.put("value",map.get("totalUnPaid"));
            data.put("name",map.get("groupByType"));
            datas.add(data);
        }
        model.addAttribute("groupByTypes",JSON.toJSONString(groupByTypes));
        model.addAttribute("datas",JSON.toJSONString(datas));
         return "chart/pieList";
    }

}
